#include "postprocessor.h"

#include <atomic>
#include <sstream>

#include "elog.h"
#include "elog_file.h"
#include "live_stacking/pixel_lib.h"
#include "live_stacking/star_align.h"
#include "live_stacking/star_stack.h"
#include "live_stacking/solar_system_stack.h"

std::atomic<int> delta_x = 0;
std::atomic<int> delta_y = 0;

int Postprocessor::alignAndStack(const cv::Mat& img_primary, const cv::Mat& img_color, cv::Mat& img_stack, cv::Mat& img_stack_all, cv::Mat& img_stack_counter) {
    if (img_color.empty())
        return -1;

    starMatch starMatches = {0};

    unsigned int match_count = 0;
    int ret = alignImages(&starMatches, img_primary, img_color, match_count, rss_metric_, angle_metric_, min_angle_, tri_n_stars_);
    if (ret < 0)
        return ret;

    delta_x = starMatches.deltaX;
    delta_y = starMatches.deltaY;
    
    stackImageNew(img_color, img_stack, img_stack_all, img_stack_counter, &starMatches);

    return 0;
}

int Postprocessor::autoStretch(cv::Mat& img) {
    unlinkedStretch(img);
    return 0;
}

int Postprocessor::sStretch(const cv::Mat& img, cv::Mat& img_result) {
    sKindStretch(img, img_result);
    return 0;
}

int Postprocessor::solarSystemStack(cv::Mat& img_primary, const cv::Mat& img_color, cv::Mat& img_stack, cv::Mat& img_stack_all, cv::Mat& img_stack_counter) {
    int ret = overlayimage(img_primary, img_color, img_stack, img_stack_all, img_stack_counter);
    return ret;
}